
--[[
本逻辑用于抽取
三合一 生成组合

卡池同时存在4种组合卡
每次三合一后去除完成组合的卡，随机加入新的组合
当一个组合10次1张都没有没有选择，也会剔除并加入新的卡
]]
local mt = {}
extend(ac.card_logic,mt)

--扣除消耗
mt.free_count = 0
mt.free_refresh = 0
function mt:can_used()
    local player = self.player
    if self._replace_card then
        print('等待替换')
        return false
    end
    if self.free_count>0 then
        self.free_count = self.free_count - 1
        return true
    end
    if player:get('木材')<100 then
        return false,'木材'
    end
    player:add('木材',-100)
    return true
end

function mt:can_refresh()
    if self.free_refresh>0 then
        self.free_refresh = self.free_refresh - 1
        return true
    end
end


--[获得逻辑]
function mt:add_card(name,index)
    local player = self.player
    local hero = player.hero
    local list = self._card_save
    self:remove_card(index)

    local skill = hero:add_skill(name,'英雄')
    list[index] = skill
    self:notify('finish',list[index])
    return skill
end

--添加符文
function mt:add_fw()
    local pool = self:get_pool()
    local temp,index = table.random(self.fw_list)
    table.remove(self.fw_list,index)
    for _,name in ipairs(temp.card_list) do
        local skill = ac.lni('skill',name)
        table.insert(pool,skill)
    end
    self.has_type = self.has_type + 1
end

--[更新卡池]
mt.has_type = 0
function mt:update_pool(skill)
    local player = self.player
    local hero = player.hero
    local name = skill and skill.bind_name
    if name then
        local item = ac.lni('item',name)
        if self:get(name)==#item.entry + 1 then
            local save = self:get_save()
            self.has_type = self.has_type - 1
            self:add_fw()
            self:set(name,0)
            
            local time = 0.5
            for a=1,6 do
                local temp = save[a]
                if temp and temp.bind_name==name then
                    temp:remove()
                    save[a] = nil
                    player:notify('动画-获得符文装备',player,temp,time)
                end
            end
            ac.wait(time*1000,function()
                local item = hero:add_item(name)
                show_item_tip(player,item,'刷新')
            end)
        end
    end

    --添加符文
    for a=self.has_type+1,4 do
        self:add_fw()
    end
end





ac.skill('抽取符文'){
    art = [[ReplaceableTextures\CommandButtons\BTNSkillz.blp]],
    tip = [[点击抽取装备碎片]],
    resume_flag = true,
    is_order = 1,
    on_add = function(self)
        local hero = self.owner
        local player = hero.owner
        self:gc(ac.loop(100,function()
            self.show_effect = player:get('木材')>=100
        end))
    end,
    on_cast_shot = function(self)
        local hero = self.owner
        local player = hero.owner
        local card = player.fw_card
        local data = card:get_list()
        if data then
            card:notify('show_select',data)
        else
            local bool,str = card:can_used()
            if bool then
                card:notify('used')
            elseif str then
                show_skill_tip(player,self,str)
            end
        end
    end,
}

ac.sync:event '同步-选择符文'(function(_,player,index)
    local card = player.fw_card
    if card then
        card:select(index)
    end
end)

ac.sync:event '同步-替换符文'(function(_,player,index)
    local card = player.fw_card
    if card then
        card:notify('replace',index)
    end
end)


ac.sync:event '同步-刷新符文'(function(_,player)
    local card = player.fw_card
    if card and card:can_refresh() then
        card:notify('used')
    end
end)

-- register_hero(function(player,hero)
--     local card = setmetatable({}, {__index = mt})
--     local skill = hero:add_skill('抽取符文','英雄',8)
--     card.player = player
--     card.fw_list = get_fw_list()
--     card.select_ui = ac['符文选择']
--     card.replace_ui = ac['符文替换']
--     card:init()

--     player.fw_card = card
--     player:set_data('拥有符文列表',card:get_save())
--     player:add('木材',100)
-- end)


